<?php

abstract class Model_Category extends ZloY_Model_CRUD{
	
	public static function create($id , $item = null){
		switch ($item){
			case 'place'  : return new Model_Category_Place($id);
			case 'event' : return new Model_Category_Event($id);
			default: return new Model_Category_Place($id);
		}
	}

	public function getAll(){
		return $this->_table->fetchAll();
	}
	
	public function delete()
	{
		$this->_table->deleteBranch($this->_row->id);
	}
	
	public function getParent()
	{
		return $this->_table->getParent($this->_row->id);
	}
	
	public function getLevel($level)
	{
		return $this->_table->fetchAll('level = ' . (int) $level);
	}
	
	public function add()
	{
		$this->_row  =  $this->_table->createChildRow((int)$this->_row->parent, $this->_row->toArray());
		return $this->save();
	}
	
	public function createChildren()
	{
		$className = get_class($this);
		$instance = new $className();
		$instance->fill(array('parent'=>$this->_row->id));
		return $instance;
	}
	public function getChildren()
	{
		return $this->_table->fetchAll('parent = ' . (int) $this->_row->id);
	}
	

}